﻿

using LeaderCCSLaserUI.Data;
using LeaderCCSLaserUI.Data.Extensions;
using LeaderCCSLaserUI.Models;
using Microsoft.EntityFrameworkCore;
using MotionRobot.Models;
using netDxf.Tables;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Windows.Forms;
using System.Xml.Linq;
using MessageBox = System.Windows.Forms.MessageBox;

namespace LeaderCCSLaserUI.ViewModels.Dialogs
{
    public class ParameterDialogViewModel : DialogViewModel
    {
        #region 变量
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private Param? systemParam;
        private int documentIndex1 = 0, documentIndex2 = 0;
        #endregion
        #region 属性
        private bool isSimulate;
        public bool IsSimulate
        {
            get { return isSimulate; }
            set { SetProperty(ref isSimulate, value); }
        }
        private string leftWeldIP;
        public string LeftWeldIP
        {
            get { return leftWeldIP; }
            set { SetProperty(ref leftWeldIP, value); }
        }
        private string rightWeldIP;
        public string RightWeldIP
        {
            get { return rightWeldIP; }
            set { SetProperty(ref rightWeldIP, value); }
        }
        private string accMul;
        public string AccMul
        {
            get { return accMul; }
            set { SetProperty(ref accMul, value); }
        }
        private string preDelay;
        public string PreDelay
        {
            get { return preDelay; }
            set { SetProperty(ref preDelay, value); }
        }
        private string leftCameraCalibDist;
        public string LeftCameraCalibDist
        {
            get { return leftCameraCalibDist; }
            set { SetProperty(ref leftCameraCalibDist, value); }
        }
        private string rightCameraCalibDist;
        public string RightCameraCalibDist
        {
            get { return rightCameraCalibDist; }
            set { SetProperty(ref rightCameraCalibDist, value); }
        }
        private bool isUseVision;
        public bool IsUseVision
        {
            get { return isUseVision; }
            set { SetProperty(ref isUseVision, value); }
        }
        private string vxyLimit;
        public string VxyLimit
        {
            get { return vxyLimit; }
            set { SetProperty(ref vxyLimit, value); }
        }
        private string flyPreDelay;
        public string FlyPreDelay
        {
            get { return flyPreDelay; }
            set { SetProperty(ref flyPreDelay, value); }
        }
        private string imagePath;
        public string ImagePath
        {
            get { return imagePath; }
            set { SetProperty(ref imagePath, value); }
        }
        private string comparisonPath;
        public string ComparisonPath
        {
            get { return comparisonPath; }
            set { SetProperty(ref comparisonPath, value); }
        }
        private int lazerDevIndex;
        public int LazerDevIndex
        {
            get { return lazerDevIndex; }
            set { SetProperty(ref lazerDevIndex, value); }
        }
        private double leftPowerBase;
        public double LeftPowerBase
        {
            get { return leftPowerBase; }
            set { SetProperty(ref leftPowerBase, value); }
        }
        private double rightPowerBase;
        public double RightPowerBase
        {
            get { return rightPowerBase; }
            set { SetProperty(ref rightPowerBase, value); }
        }
        
        private string leftCameraExposureTime;
        public string LeftCameraExposureTime
        {
            get { return leftCameraExposureTime; }
            set { SetProperty(ref leftCameraExposureTime, value); }
        }
        private string rightCameraExposureTime;
        public string RightCameraExposureTime
        {
            get { return rightCameraExposureTime; }
            set { SetProperty(ref rightCameraExposureTime, value); }
        }
        private string leftFlyGrabPreDelay;
        public string LeftFlyGrabPreDelay
        {
            get { return leftFlyGrabPreDelay; }
            set { SetProperty(ref leftFlyGrabPreDelay, value); }
        }
        private string rightFlyGrabPreDelay;
        public string RightFlyGrabPreDelay
        {
            get { return rightFlyGrabPreDelay; }
            set { SetProperty(ref rightFlyGrabPreDelay, value); }
        }
        private string zLimit;
        public string ZLimit
        {
            get { return zLimit; }
            set { SetProperty(ref zLimit, value); }
        }
        private bool isVisionResultConfirm;
        public bool IsVisionResultConfirm
        {
            get { return isVisionResultConfirm; }
            set { SetProperty(ref isVisionResultConfirm, value); }
        }
        private ObservableCollection<ParamScaleDisp> pointList1 = new ObservableCollection<ParamScaleDisp>();
        public ObservableCollection<ParamScaleDisp> PointList1
        {
            get { return pointList1; }
            set { SetProperty(ref pointList1, value); }
        }
        private ObservableCollection<ParamScaleDisp> pointList2 = new ObservableCollection<ParamScaleDisp>();
        public ObservableCollection<ParamScaleDisp> PointList2
        {
            get { return pointList2; }
            set { SetProperty(ref pointList2, value); }
        }
        private bool isCameraScanBarcode;
        public bool IsCameraScanBarcode
        {
            get { return isCameraScanBarcode; }
            set { SetProperty(ref isCameraScanBarcode, value); }
        }

        #endregion
        #region 方法绑定
        private DelegateCommand<object> simulateSwitchCommand;
        public DelegateCommand<object> SimulateSwitchCommand =>
            simulateSwitchCommand ?? (simulateSwitchCommand = new DelegateCommand<object>(ExecuteSimulateSwitchCommand));
        private DelegateCommand<object> textBoxLostFocusCommand;
        public DelegateCommand<object> TextBoxLostFocusCommand =>
            textBoxLostFocusCommand ?? (textBoxLostFocusCommand = new DelegateCommand<object>(ExecuteTextBoxLostFocusCommand));
        private DelegateCommand<object> useVisionSwitchCommand;
        public DelegateCommand<object> UseVisionSwitchCommand =>
            useVisionSwitchCommand ?? (useVisionSwitchCommand = new DelegateCommand<object>(ExecuteUseVisionSwitchCommand));
        private DelegateCommand<object> openDirectoryCommand;
        public DelegateCommand<object> OpenDirectoryCommand =>
            openDirectoryCommand ?? (openDirectoryCommand = new DelegateCommand<object>(ExecuteOpenDirectoryCommand));
        private DelegateCommand<object> selectLazerDevCommand;
        public DelegateCommand<object> SelectLazerDevCommand =>
            selectLazerDevCommand ?? (selectLazerDevCommand = new DelegateCommand<object>(ExecuteSelectLazerDevCommand));
        private DelegateCommand<object> getValueCommand;
        public DelegateCommand<object> GetValueCommand =>
            getValueCommand ?? (getValueCommand = new DelegateCommand<object>(ExecuteGetValueCommand));
        private DelegateCommand<object> visionResultConfirmSwitchCommand;
        public DelegateCommand<object> VisionResultConfirmSwitchCommand =>
            visionResultConfirmSwitchCommand ?? (visionResultConfirmSwitchCommand = new DelegateCommand<object>(ExecuteVisionResultConfirmSwitchCommand));
        private DelegateCommand<object> selectDocument1Command;
        public DelegateCommand<object> SelectDocument1Command =>
            selectDocument1Command ?? (selectDocument1Command = new DelegateCommand<object>(ExecuteSelectDocument1Command));
        private DelegateCommand<object> selectDocument2Command;
        public DelegateCommand<object> SelectDocument2Command =>
            selectDocument2Command ?? (selectDocument2Command = new DelegateCommand<object>(ExecuteSelectDocument2Command));
        private DelegateCommand<object> pointList1RowEditEndingCommand;
        public DelegateCommand<object> PointList1RowEditEndingCommand =>
            pointList1RowEditEndingCommand ?? (pointList1RowEditEndingCommand = new DelegateCommand<object>(ExecutePointList1RowEditEndingCommand));
        private DelegateCommand<object> pointList2RowEditEndingCommand;
        public DelegateCommand<object> PointList2RowEditEndingCommand =>
            pointList2RowEditEndingCommand ?? (pointList2RowEditEndingCommand = new DelegateCommand<object>(ExecutePointList2RowEditEndingCommand));
        private DelegateCommand<object> cameraScanBarcodeSwitchCommand;
        public DelegateCommand<object> CameraScanBarcodeSwitchCommand =>
            cameraScanBarcodeSwitchCommand ?? (cameraScanBarcodeSwitchCommand = new DelegateCommand<object>(ExecuteCameraScanBarcodeSwitchCommand));
     

        void ExecuteCameraScanBarcodeSwitchCommand(object obj)
        {
            if (obj is bool v1)
            {
                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "IsCameraScanBarcode");
                if (_mParam != null)
                {
                    _mParam.Value = v1.ToString();
                    mdb.SaveChanges();
                }
            }
        }
        void ExecutePointList2RowEditEndingCommand(object obj)
        {
            if (obj is ParamScaleDisp disp)
            {
                try
                {
                    using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                    var _param = mdb.ParamScales.FirstOrDefault(v => v.Hand == 1 && v.LazerDocumentIndex == documentIndex2 && v.Name == disp.Name);
                    if (_param != null)
                    {
                        _param.PValue = disp.PValue;
                        _param.UpLimit = disp.UpLimit;
                        _param.DownLimit = disp.DownLimit;
                        mdb.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        void ExecutePointList1RowEditEndingCommand(object obj)
        {
            if (obj is ParamScaleDisp disp)
            {
                try
                {
                    using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                    var _param = mdb.ParamScales.FirstOrDefault(v => v.Hand == 0 && v.LazerDocumentIndex == documentIndex1 && v.Name == disp.Name);
                    if (_param != null)
                    {
                        _param.PValue = disp.PValue;
                        _param.UpLimit = disp.UpLimit;
                        _param.DownLimit = disp.DownLimit;
                        mdb.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        void ExecuteSelectDocument2Command(object obj)
        {
            if (int.TryParse(obj.ToString(), out var index))
            {
                documentIndex2 = index;
                LoadPointList2(index);
            }
        }
        void ExecuteSelectDocument1Command(object obj)
        {
            if (int.TryParse(obj.ToString(),out var index))
            {
                documentIndex1 = index;
                LoadPointList1(index);
            }
        }
        void ExecuteVisionResultConfirmSwitchCommand(object obj)
        {
            if (obj is bool v1)
            {
                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "IsVisionResultConfirm");
                if (_mParam != null)
                {
                    _mParam.Value = v1.ToString();
                    mdb.SaveChanges();
                }
            }
        }
        void ExecuteGetValueCommand(object obj)
        {
            if (obj is string item)
            {
                switch (item)
                {
                    case "LeftPowerBase":
                        {
                            var adcv = GTSCard.GetAdc(1);
                            systemParam.LeftPowerBase = LeftPowerBase = Math.Round(adcv / 4 * 1000, 0);                    
                        }
                        break;
                    case "RightPowerBase":
                        {
                            var adcv = GTSCard.GetAdc(2);
                            systemParam.RightPowerBase = RightPowerBase = Math.Round(adcv / 4 * 1000, 0);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        void ExecuteSelectLazerDevCommand(object obj)
        {
            int _index = int.Parse(obj.ToString());
            systemParam.LazerDevIndex = _index;
        }
        void ExecuteOpenDirectoryCommand(object obj)
        {
            using (var fbd = new FolderBrowserDialog())
            {
                System.Windows.Forms.DialogResult result = fbd.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    switch (obj.ToString())
                    {
                        case "Image":
                            {
                                ImagePath = fbd.SelectedPath;
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ImagePath");
                                if (_mParam != null)
                                {
                                    _mParam.Value = fbd.SelectedPath;
                                    _mParam.Type = typeof(string).Name;
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "Comparison":
                            {
                                ComparisonPath = fbd.SelectedPath;
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ComparisonPath");
                                if (_mParam != null)
                                {
                                    _mParam.Value = fbd.SelectedPath;
                                    _mParam.Type = typeof(string).Name;
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        void ExecuteUseVisionSwitchCommand(object obj)
        {
            if (obj is bool r)
            {
                systemParam.IsUseVision = r;
            }
        }
        void ExecuteTextBoxLostFocusCommand(object obj)
        {
            if (obj is string str)
            {
                switch (str)
                {
                    case "LeftWeldIP":
                        systemParam.LeftWeldIP = LeftWeldIP;
                        break;
                    case "RightWeldIP":
                        systemParam.RightWeldIP = RightWeldIP;
                        break;
                    case "LeftPowerBase":
                        systemParam.LeftPowerBase = LeftPowerBase;
                        break;
                    case "RightPowerBase":
                        systemParam.RightPowerBase = RightPowerBase;
                        break;
                    case "AccMul":
                        {
                            if (double.TryParse(AccMul, out var v))
                            {
                                systemParam.AccMul = v;
                            }
                        }
                        break;
                    case "PreDelay":
                        {
                            if (double.TryParse(PreDelay, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "PreDelay");
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "PreDelay",
                                        Value = PreDelay,
                                        Hand = 0,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "VxyLimit":
                        {
                            if (double.TryParse(VxyLimit, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "VxyLimit");
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "VxyLimit",
                                        Value = VxyLimit,
                                        Hand = 0,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "FlyPreDelay":
                        {
                            if (double.TryParse(FlyPreDelay, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyPreDelay");
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "FlyPreDelay",
                                        Value = FlyPreDelay,
                                        Hand = 0,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "LeftCameraCalibDist":
                        {
                            if (double.TryParse(LeftCameraCalibDist, out var v))
                            {
                                systemParam.LeftCameraCalibDist = v;
                            }
                        }
                        break;
                    case "RightCameraCalibDist":
                        {
                            if (double.TryParse(RightCameraCalibDist, out var v))
                            {
                                systemParam.RightCameraCalibDist = v;
                            }
                        }
                        break;                                       
                    case "LeftCameraExposureTime":
                        {
                            if (double.TryParse(LeftCameraExposureTime, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "CameraExposureTime" && m.Hand == 0);
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "CameraExposureTime",
                                        Value = LeftCameraExposureTime,
                                        Hand = 0,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "RightCameraExposureTime":
                        {
                            if (double.TryParse(RightCameraExposureTime, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "CameraExposureTime" && m.Hand == 1);
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "CameraExposureTime",
                                        Value = RightCameraExposureTime,
                                        Hand = 1,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "LeftFlyGrabPreDelay":
                        {
                            if (double.TryParse(LeftFlyGrabPreDelay, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyGrabPreDelay" && m.Hand == 0);
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "FlyGrabPreDelay",
                                        Value = LeftFlyGrabPreDelay,
                                        Hand = 0,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "RightFlyGrabPreDelay":
                        {
                            if (double.TryParse(RightFlyGrabPreDelay, out var v))
                            {
                                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyGrabPreDelay" && m.Hand == 1);
                                if (_mParam != null)
                                {
                                    _mParam.Value = v.ToString();
                                }
                                else
                                {
                                    mdb.MParams.Add(new MParam
                                    {
                                        Name = "FlyGrabPreDelay",
                                        Value = RightFlyGrabPreDelay,
                                        Hand = 1,
                                        Type = typeof(double).Name
                                    });
                                }
                                mdb.SaveChanges();
                            }
                        }
                        break;
                    case "ZLimit":
                        {
                            if (double.TryParse(ZLimit, out var v))
                            {
                                if (v > 0)
                                {
                                    using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                                    var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ZLimit");
                                    if (_mParam != null)
                                    {
                                        _mParam.Value = v.ToString();
                                    }
                                    else
                                    {
                                        mdb.MParams.Add(new MParam
                                        {
                                            Name = "ZLimit",
                                            Value = ZLimit,
                                            Hand = 0,
                                            Type = typeof(double).Name
                                        });
                                    }
                                    mdb.SaveChanges();
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        void ExecuteSimulateSwitchCommand(object obj)
        {
            if (obj is bool r)
            {
                systemParam.IsSimulate = r;
            }
        }
        #endregion
        #region 导航
        public override void OnDialogClosed()
        {
            SavedParam();
            aggregator.SendMessage("ReloadParam", "Run");
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            LoadParam();
            IsSimulate = systemParam.IsSimulate;
            IsUseVision = systemParam.IsUseVision;
            LeftWeldIP = systemParam.LeftWeldIP;
            RightWeldIP = systemParam.RightWeldIP;
            LazerDevIndex = systemParam.LazerDevIndex;
            AccMul = systemParam.AccMul.ToString();
            LeftCameraCalibDist = systemParam.LeftCameraCalibDist.ToString();
            RightCameraCalibDist = systemParam.RightCameraCalibDist.ToString();
            LeftPowerBase = systemParam.LeftPowerBase;
            RightPowerBase = systemParam.RightPowerBase;
            using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
            var _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "PreDelay");
            if (_mParam != null)
            {
                PreDelay = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "VxyLimit");
            if (_mParam != null)
            {
                VxyLimit = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyPreDelay");
            if (_mParam != null)
            {
                FlyPreDelay = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ComparisonPath");
            if (_mParam != null)
            {
                ComparisonPath = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ImagePath");
            if (_mParam != null)
            {
                ImagePath = _mParam.Value;
            }
            
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "CameraExposureTime" && m.Hand == 0);
            if (_mParam != null)
            {
                LeftCameraExposureTime = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "CameraExposureTime" && m.Hand == 1);
            if (_mParam != null)
            {
                RightCameraExposureTime = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyGrabPreDelay" && m.Hand == 0);
            if (_mParam != null)
            {
                LeftFlyGrabPreDelay = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "FlyGrabPreDelay" && m.Hand == 1);
            if (_mParam != null)
            {
                RightFlyGrabPreDelay = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "ZLimit");
            if (_mParam != null)
            {
                ZLimit = _mParam.Value;
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "IsVisionResultConfirm");
            if (_mParam != null)
            {
                if (bool.TryParse(_mParam.Value,out var _v))
                {
                    IsVisionResultConfirm = _v;
                }
                else
                {
                    IsVisionResultConfirm = false;
                    _mParam.Value = IsVisionResultConfirm.ToString();
                    mdb.SaveChanges();
                }
            }
            else
            {
                IsVisionResultConfirm = false;
                mdb.MParams.Add(new MParam { 
                    Name = "IsVisionResultConfirm",
                    Value = IsVisionResultConfirm.ToString(),
                    Type = typeof(bool).Name,
                    Hand = 0
                });
                mdb.SaveChanges();
            }
            _mParam = mdb.MParams.FirstOrDefault(m => m.Name == "IsCameraScanBarcode");
            if (_mParam != null)
            {
                if (bool.TryParse(_mParam.Value, out var _v))
                {
                    IsCameraScanBarcode = _v;
                }
                else
                {
                    IsCameraScanBarcode = false;
                    _mParam.Value = IsCameraScanBarcode.ToString();
                    mdb.SaveChanges();
                }
            }
            else
            {
                IsCameraScanBarcode = false;
                mdb.MParams.Add(new MParam
                {
                    Name = "IsCameraScanBarcode",
                    Value = IsCameraScanBarcode.ToString(),
                    Type = typeof(bool).Name,
                    Hand = 0
                });
                mdb.SaveChanges();
            }
            
            LoadPointList1(0);
            LoadPointList2(0);
        }
        #endregion
        #region 构造函数
        public ParameterDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "机台参数";
        }
        #endregion
        #region 功能函数
        void LoadParam()
        {
            try
            {
                //Json序列化，从文件读取
                string jsonString = File.ReadAllText(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Param.json"));
                systemParam = JsonConvert.DeserializeObject<Param>(jsonString);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
        void SavedParam()
        {
            try
            {
                string jsonString = JsonConvert.SerializeObject(systemParam, Formatting.Indented);
                File.WriteAllText(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Param.json"), jsonString);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

        }
        void LoadPointList1(int document)
        {
            PointList1.Clear();
            try
            {
                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                var _params = mdb.ParamScales.Where(v => v.Hand == 0 && v.LazerDocumentIndex == document);
                if (_params.Any())
                {
                    _params.ToList().ForEach(p => {
                        PointList1.Add(new ParamScaleDisp {
                            Name = p.Name,
                            PValue = p.PValue,
                            UpLimit = p.UpLimit,
                            DownLimit = p.DownLimit 
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void LoadPointList2(int document)
        {
            PointList2.Clear();
            try
            {
                using var mdb = new MDbContext(systemParam.PartList[systemParam.PartIndex]);
                var _params = mdb.ParamScales.Where(v => v.Hand == 1 && v.LazerDocumentIndex == document);
                if (_params.Any())
                {
                    _params.ToList().ForEach(p => {
                        PointList2.Add(new ParamScaleDisp
                        {
                            Name = p.Name,
                            PValue = p.PValue,
                            UpLimit = p.UpLimit,
                            DownLimit = p.DownLimit
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }
    public class ParamScaleDisp
    {
        public string Name { get; set; }//Power;Speed;FocalLength
        public double PValue { get; set; }
        public double UpLimit { get; set; }
        public double DownLimit { get; set; }
    }
}
